import { CanActivate, ExecutionContext, Injectable } from '@nestjs/common';
import { Observable } from 'rxjs';

import { Reflector } from '@nestjs/core'
import type { Request } from 'express'


@Injectable()
export class RolesGuard implements CanActivate {
  constructor(private Reflector: Reflector){}
  canActivate(
    context: ExecutionContext,
  ): boolean | Promise<boolean> | Observable<boolean> {
    // 通过守卫进行权限拦截
    const rolesArr: string[] = this.Reflector.get('roles', context.getHandler());
    const request = context.switchToHttp().getRequest<Request>();
    console.log('走了守卫啦....', rolesArr, request.query.role);
    if (rolesArr.includes(request.query.role as string)) {
      return true
    }
    return false;
  }
}
